package thread;

/**
 * 通过异常方法终止线程
 */
public class InterruptThread {
    public static void main(String[] args) throws InterruptedException {


        Thread thread = new Thread(new MyRunnable());
        thread.start();
        thread.interrupt();
        System.out.println(thread.isInterrupted());
        Thread.sleep(1000);
    }


}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        try {
            for (int i = 0; i < 100000000; i++) {
                if (Thread.currentThread().isInterrupted()) {
                    throw new InterruptedException("线程终止");
                } else {
                    System.out.println(Thread.currentThread().getName() + "=>" + i);
                }
            }
        } catch (InterruptedException e) {
            System.out.println("捕获到异常，"+e.getMessage());
        }

    }
}